package business;
import java.io.Serializable;
import java.util.List;

import model.Evento;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

import util.HibernateUtil;

@SuppressWarnings("deprecation")
public class EventoDAO implements Serializable {
	private static final long serialVersionUID = -6326907834208009905L;
	static EventoDAO instance;
	
	public static EventoDAO getInstance()
	{
		if(instance == null)
			instance = new EventoDAO();
		return instance;
	}
	public EventoDAO()
	{
	}
	public Evento get(int id) 
	{		
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		Evento item = (Evento)session.get(Evento.class,id);		
		tr.commit();
		return item;
	}
	public void salvar(Evento evento)
	{
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		session.saveOrUpdate(evento);
		tr.commit();
		return;
	}
	public  void remover(int id)
	{
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		session.delete(session.get(Evento.class, id));
		tr.commit();
		return ;
	}
	@SuppressWarnings("unchecked")
	public List<Evento> listar() 
	{
		List<Evento> lista;
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		Criteria cr = session.createCriteria(Evento.class);
		cr.addOrder(Order.asc("descricao"));
		lista = cr.list();
		tr.commit();
		return lista;
	}
	@SuppressWarnings({ "unchecked" })
	public List<Evento> listar(String descricao) 
	{
		List<Evento> lista;
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		Criteria cr = session.createCriteria(Evento.class);
		cr.add(Expression.ilike("descricao", descricao));
		cr.addOrder(Order.asc("descricao"));
		lista = cr.list();
		tr.commit();
		return lista;
	}
}